package 数字字符串格式化.src.main;

public class Main {
    public static String solution(String s) {

        // write code here
        int len = s.length();
        s = removeLeadingZeros(s);
        String s1 = getSubstringBeforeDot(s);
        String s2 = getSubstringAfterDot(s);
        String s3 = insertCommasEveryThreeDigits(s1);
        String s4 = s3 + s2;


        return s4;
    }

    public static String removeLeadingZeros(String str) {
        return str.replaceFirst("^0+", "");
    }
    public static String getSubstringAfterDot(String str) {
        int dotIndex = str.indexOf('.');//indexOf
        if (dotIndex != -1) {
            return str.substring(dotIndex, str.length());
        } else {
            return ""; // 如果没有找到 .，则返回原字符串
        }
    }
    public static String getSubstringBeforeDot(String str) {
        int dotIndex = str.indexOf('.');//indexOf
        if (dotIndex != -1) {
            return str.substring(0, dotIndex);
        } else {
            return str; // 如果没有找到 .，则返回原字符串
        }
    }

    public static String insertCommasEveryThreeDigits(String str) {
        StringBuilder sb = new StringBuilder();
        int length = str.length();

        for (int i = length - 1, count = 0; i >= 0; i--, count++) {
            sb.append(str.charAt(i));
            if (count % 3 == 2 && i != 0) {
                sb.append(',');
            }
        }

        return sb.reverse().toString();
    }

    public static void main(String[] args) {
//        System.out.println(solution("1294512.12412"));
//        System.out.println(solution("0000123456789.99"));
//        System.out.println(solution("987654321"));
        System.out.println(solution("1294512.12412").equals("1,294,512.12412"));
        System.out.println(solution("0000123456789.99").equals("123,456,789.99"));
        System.out.println(solution("987654321").equals("987,654,321"));
    }
}